package cn.mygweb.common.tasks;

import java.util.concurrent.Future;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

/** 
* <p>Title: AsyncTask.java</p>
* <p>Description: 异步执行的任务
* 	类要使用@Component注解让spring扫描，方法要使用@Async注解开启异步</p>  
* @author gxb 
* @date 2019年5月23日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Component
public class AsyncTask {

	@Async
	public Future<Boolean> doTask1() throws Exception{
		long start = System.currentTimeMillis();
		Thread.sleep(1000);
		long end = System.currentTimeMillis();
		System.out.println("任务1耗时" + (end - start) + "毫秒");
		return new AsyncResult<>(true);
	}
	
	@Async
	public Future<Boolean> doTask2() throws Exception{
		long start = System.currentTimeMillis();
		Thread.sleep(800);
		long end = System.currentTimeMillis();
		System.out.println("任务2耗时" + (end - start) + "毫秒");
		return new AsyncResult<>(true);
	}
	
	@Async
	public Future<Boolean> doTask3() throws Exception{
		long start = System.currentTimeMillis();
		Thread.sleep(500);
		long end = System.currentTimeMillis();
		System.out.println("任务3耗时" + (end - start) + "毫秒");
		return new AsyncResult<>(true);
	}
	
}
